package com.spzx.order.controller;

import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.rabbit.constants.MqConst;
import com.spzx.common.rabbit.service.RabbitService;
import com.spzx.order.configure.DeadLetterMqConfig;
import com.spzx.order.configure.DelayedMqConfig;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Tag(name="测试MQ")
@RestController
@RequestMapping("/mq")
public class MqController extends BaseController {

    @Autowired
    RabbitService rabbitService;

    @Operation(summary = "测试普通消息发送")
    @GetMapping("/sendMessage")
    public AjaxResult sendMessage(){
        //异步发送消息
        rabbitService.sendMessage(MqConst.EXCHANGE_TEST,MqConst.ROUTING_TEST,"hello");
        return success();
    }



    @Operation(summary = "发送确认消息")
    @GetMapping("/sendConfirmMessage")
    public AjaxResult sendConfirmMessage()
    {
        rabbitService.sendMessage(MqConst.EXCHANGE_TEST, MqConst.ROUTING_CONFIRM, "hello, confirm"); //正常
        //rabbitService.sendMessage("spzx-test111", MqConst.ROUTING_CONFIRM, "hello, confirm"); //交换机名称错误。
        //rabbitService.sendMessage(MqConst.EXCHANGE_TEST, "spzx-test1111", "hello, confirm"); //发送路由key与绑定路由key不一致
        return success();
    }


    /**
     * 消息发送延迟消息：基于死信实现
     */
    @Operation(summary = "发送延迟消息：基于死信实现")
    @GetMapping("/sendDeadLetterMsg")
    public AjaxResult sendDeadLetterMsg() {
        rabbitService.sendMessage(DeadLetterMqConfig.exchange_dead, DeadLetterMqConfig.routing_dead_1, "我是延迟消息");
        return success();
    }


    @Operation(summary = "发送延迟消息：基于延迟插件")
    @GetMapping("/sendDelayMsg")
    public AjaxResult sendDelayMsg() {
        //调用工具方法发送延迟消息
        int delayTime = 10; //希望延迟10秒再被消费。
        rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay, DelayedMqConfig.routing_delay, "我是延迟消息", delayTime);
        //延迟消息发送到交换机，立马执行回退回调。10秒后转发队列时，routingKey错的，也不再执行回退了。
        //rabbitService.sendDelayMessage(DelayedMqConfig.exchange_delay, "xxxx", "我是延迟消息", delayTime);

        return success();
    }
}
